package com.weishi.project.sport.home.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishi.common.enums.TrainingProgramEnum;
import com.weishi.common.utils.StringUtils;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.project.api.ret.AnnualPlanRet;
import com.weishi.project.api.ret.NaturalYearRet;
import com.weishi.project.api.ret.WeekTreeRet;
import com.weishi.project.sport.home.service.SportHomeService;
import com.weishi.project.sport.home.vo.*;
import com.weishi.project.sport.trainingdata.service.TrainingDataService;
import com.weishi.project.sport.trainingdata.vo.ReturnMonthVO;
import com.weishi.project.sport.trainingdata.vo.TypeValueVO;
import com.weishi.project.sport.trainingdata.vo.WaterRankingVO;
import com.weishi.project.sport.util.PageUtil;
import com.weishi.project.system.service.ISysDictDataService;
import com.weishi.project.training.domain.*;
import com.weishi.project.training.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 运动员 首页信息模块 接口实现类
 */
@Service
public class SportHomeServiceImpl implements SportHomeService {

    /**
     * 运动员表 接口
     */
    @Autowired
    private ISportsmanService sportsmanService;

    /**
     * 运动员 训练数据模块 接口
     */
    @Autowired
    private TrainingDataService trainingDataService;

    /**
     * 今日训练数据 接口
     */
    @Autowired
    private ITrainingDayService trainingDayService;

    /**
     * 自然年接口
     */
    @Autowired
    private INaturalYearService naturalYearService;

    /**
     * 训练周期 接口
     */
    @Autowired
    private IAnnualPlanService annualPlanService;

    /**
     * 训练周接口
     */
    @Autowired
    private ITrainingWeekService trainingWeekService;

    /**
     * 训练成绩表 接口
     */
    @Autowired
    private ITrainingAchievementService trainingAchievementService;

    /**
     * 请假表 接口
     */
    @Autowired
    private ILeaveFormService leaveFormService;

    /**
     * 字典子表 接口
     */
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 航道接口
     */
    @Autowired
    private ITrainingGroupChannelService trainingGroupChannelService;



    /**
     * 运动员 -- 获取首页个人信息
     */
    @Override
    public AjaxResult getMineDate(Integer sportId) {
        try {
            Sportsman sportsman = sportsmanService.getById(sportId);
            MineDateVO mineDateVO = new MineDateVO();
            StringUtils.copyProperties(sportsman , mineDateVO);
            // 计算年龄
            mineDateVO.setAge(StringUtils.getAgeByBirth(sportsman.getBirthday()));

            // 重新查询学历
            List<TypeValueVO> windSpeed = dictDataService.getTypeValue("education");
            windSpeed.forEach( pp-> {
                if(mineDateVO.getEducation().equals(pp.getDictValue())){
                    mineDateVO.setEducation(pp.getDictLabel());
                }
            });

            return AjaxResult.success(mineDateVO);
        }catch (Exception e){
            System.err.println(e.getMessage());
        }
        return AjaxResult.success();
    }


    /**
     * 运动员 -- 根据条件查询首页数据
     */
    @Override
    public AjaxResult getMineHomeData() {
        return null;
    }



    private List<WeekTreeRet> changeTree(List<NaturalYearRet> list) {
        List<WeekTreeRet> weekTreeRets = new ArrayList<>();
        for(NaturalYearRet naturalYearRet : list){
            WeekTreeRet weekTreeRet1 = new WeekTreeRet();
            weekTreeRet1.setId(naturalYearRet.getId());
            weekTreeRet1.setName(naturalYearRet.getName());
            List<WeekTreeRet> weekTreeRets1 = new ArrayList<>();
            weekTreeRet1.setChildren(weekTreeRets1);
            List<AnnualPlanRet> annualPlans = naturalYearRet.getAnnualPlans();
            for(AnnualPlanRet annualPlanRet : annualPlans){
                WeekTreeRet weekTreeRet2 = new WeekTreeRet();
                weekTreeRet2.setId(annualPlanRet.getId());
                weekTreeRet2.setName(annualPlanRet.getCycleName());
                weekTreeRets1.add(weekTreeRet2);
                List<TrainingWeek> trainingWeeks = annualPlanRet.getTrainingWeeks();
                List<WeekTreeRet> weekTreeRets2 =  new ArrayList<>();
                weekTreeRet2.setChildren(weekTreeRets2);
                for(TrainingWeek trainingWeek : trainingWeeks){
                    WeekTreeRet weekTreeRet3 = new WeekTreeRet();
                    weekTreeRet3.setId(trainingWeek.getId());
                    weekTreeRet3.setName("第"+ trainingWeek.getWeek() + "周");
                    weekTreeRets2.add(weekTreeRet3);
                }
            }

            weekTreeRets.add(weekTreeRet1);
        }
        return weekTreeRets;
    }
    /**
     * 运动员 -- 查询自然年
     */
    @Override
    public AjaxResult getYear() {
        return AjaxResult.success(naturalYearService.selectNaturalYearRetListVO(true));
    }


    /**
     * 运动员 -- 本周训练数据
     */
    @Override
    public AjaxResult getThisWeekTrainingData(String day) {
        return AjaxResult.success(trainingDayService.getThisWeekTrainingDataVO(day));
    }


    /**
     * 运动员 -- 本周训练数据 -- 查询日期
     */
    @Override
    public AjaxResult getQueryDate() {
        WeekVO weekVO = new WeekVO();

        String day = StringUtils.substring(StringUtils.getCurrentDate() , 0 , 4);
        List<QueryDateVO> queryDateVOList = trainingWeekService.getQueryDate(day , null);
        if(queryDateVOList.size() == 0){
            return AjaxResult.success("暂无数据！");
        }else{
            List<QueryDateVO> queryDateVOList1 = new ArrayList<>();
            queryDateVOList.forEach( p-> {
                // 判断当前日期是否在两个日期之间
                if(StringUtils.isItCurrentlyInTwoTimes(new Date(), p.getStartTime() , p.getEndTime()) == false){

                }else{
                    queryDateVOList1.add(p);
                }
            });

            //本周数据
            weekVO.setThisWeek(queryDateVOList1);

            // 上周数据
            if(queryDateVOList.get(0).getWeek() -1 >0){
                weekVO.setLastWeek(trainingWeekService.getQueryDate("" , queryDateVOList.get(0).getWeek() - 1));
            }

            // 下周数据
            if(trainingWeekService.list(Wrappers.<TrainingWeek>lambdaQuery().orderByDesc(TrainingWeek::getWeek)).get(0).getWeek() < trainingWeekService.list(Wrappers.<TrainingWeek>lambdaQuery().orderByDesc(TrainingWeek::getWeek)).get(0).getWeek()+1){
                weekVO.setNextWeek(trainingWeekService.getQueryDate("" , queryDateVOList.get(0).getWeek() + 1));
            }

            return AjaxResult.success(weekVO);
        }
    }


    /**
     * 运动员 -- 根据训练分类查询总计
     */
    @Override
    public AjaxResult getClassifiedTotal(Integer userId , String day) {
        List<LeaveForm> list = leaveFormService.list(Wrappers.<LeaveForm>lambdaQuery()
                .like(LeaveForm::getEndTime , day).groupBy(LeaveForm::getEndTime));
        if(list.size() > 0){
            return AjaxResult.success();
        }else{

            // 根据成绩表查询今天参加的所有课程 id
            List<Integer> idList = trainingAchievementService.getIdList(userId , day);

            // 查询今天的课程 所有的热身距离然后相加
            Double warmUpKilometers = 0.0;
            if(idList.size() > 0){
                List<TrainingDay> trainingDayList = trainingDayService.list(Wrappers.<TrainingDay>lambdaQuery()
                        .in(TrainingDay::getId , idList));
                for (int i = 0; i<trainingDayList.size(); i++){
                    warmUpKilometers += trainingDayList.get(i).getWarmUpKilometers();
                }
            }

            List<ClassifiedTotalVO> totalList = trainingDayService.getClassifiedTotal(day , userId);
            for (int i = 0; i<totalList.size(); i++){
                if(totalList.get(i).getFirstLevel().equals(TrainingProgramEnum.LANDSPECIFICTRAININGDYNAMOMETER.getKey())){
                    warmUpKilometers += totalList.get(i).getTotalKilometer();
                    totalList.get(i).setTotalKilometer(warmUpKilometers);
                }
            }
            return AjaxResult.success(totalList);
        }
    }


    /**
     * 计算百分比方法
     */
    public static String getPercent(int x, int y) {
        double d1 = x * 1.0;
        double d2 = y * 1.0;
        NumberFormat percentInstance = NumberFormat.getPercentInstance();
        // 设置保留几位小数，这里设置的是保留两位小数
        percentInstance.setMinimumFractionDigits(1);
        return percentInstance.format(d1 / d2);
    }

    /**
     * 运动员 -- 首页 --根据年查询各科训练时间占比
     */
    @Override
    public AjaxResult getHomeTimeProportion(Integer userId , String years) {
        List<Integer> list = new ArrayList<>();
        List<ClassifiedTotalVO> classifiedTotalVOList = trainingDayService.getClassifiedTotal(years , userId);
        if(classifiedTotalVOList.size() == 0){
            return AjaxResult.success("暂无数据！");
        }else{
            classifiedTotalVOList.forEach( p-> {
                int totalTime = p.getTotalTime().intValue();
                list.add(totalTime);
            });

            // 运动员 -- 首页 --根据年查询各科训练时间占比
            // 查询除了 水上专项训练和陆上专项训练的其它数据
            List<ClassifiedTotalVO> classifiedTotalVOListTwo = trainingDayService.getClassifiedTotalTwo(years);
            if(classifiedTotalVOListTwo.size() > 0)classifiedTotalVOListTwo.forEach( p->{
                classifiedTotalVOList.add(p);
            });
            return AjaxResult.success(classifiedTotalVOList);
        }
    }


    /**
     * 运动员 -- 首页 --根据年查询各科训练时间占比
     * 查询除了 水上专项训练和陆上专项训练的其它数据
     */
    public List<ClassifiedTotalVO> adds(List<ClassifiedTotalVO> classReturn , String years){
        // 运动员 -- 首页 --根据年查询各科训练时间占比
        // 查询除了 水上专项训练和陆上专项训练的其它数据
        List<ClassifiedTotalVO> classifiedTotalVOListTwo = trainingDayService.getClassifiedTotalTwo(years);

        // 排序
        List<ClassifiedTotalVO> sortingList = new ArrayList<>();
        classReturn.forEach( p-> {
            if(p.getFirstLevelName().equals("水上专项训练")){
                // 查出除了 水上专项训练和陆上专项训练的其它数据
                if(classifiedTotalVOListTwo.size() > 0)classifiedTotalVOListTwo.forEach( s->{
                    if(s.getFirstLevelName().equals("水上专项训练")){
                        StringUtils.copyProperties(s , p);
                    }
                });
            }
        });
        classReturn.forEach( p-> {
            if(p.getFirstLevelName().equals("陆上专项训练(测功仪)")){
                // 查出除了 水上专项训练和陆上专项训练的其它数据
                if(classifiedTotalVOListTwo.size() > 0)classifiedTotalVOListTwo.forEach( s->{
                    if(s.getFirstLevelName().equals("陆上专项训练(测功仪)")){
                        StringUtils.copyProperties(s , p);
                    }
                });
            }
        });
        classReturn.forEach( p-> {
            if(p.getFirstLevelName().equals("陆上身体素质训练")){
                // 查出除了 水上专项训练和陆上专项训练的其它数据
                if(classifiedTotalVOListTwo.size() > 0)classifiedTotalVOListTwo.forEach( s->{
                    if(s.getFirstLevelName().equals("陆上身体素质训练")){
                        StringUtils.copyProperties(s , p);
                    }
                });
            }
        });
        classReturn.forEach( p-> {
            if(p.getFirstLevelName().equals("陆上有氧训练")){
                // 查出除了 水上专项训练和陆上专项训练的其它数据
                if(classifiedTotalVOListTwo.size() > 0)classifiedTotalVOListTwo.forEach( s->{
                    if(s.getFirstLevelName().equals("陆上有氧训练")){
                        StringUtils.copyProperties(s , p);
                    }
                });
            }
        });
        classReturn.forEach( p-> {
            if(p.getFirstLevelName().equals("再生训练")){
                // 查出除了 水上专项训练和陆上专项训练的其它数据
                if(classifiedTotalVOListTwo.size() > 0)classifiedTotalVOListTwo.forEach( s->{
                    if(s.getFirstLevelName().equals("再生训练")){
                        StringUtils.copyProperties(s , p);
                    }
                });
            }
        });
        classReturn.forEach( p-> {
            if(p.getFirstLevelName().equals("伤病治疗")){
                // 查出除了 水上专项训练和陆上专项训练的其它数据
                if(classifiedTotalVOListTwo.size() > 0)classifiedTotalVOListTwo.forEach( s->{
                    if(s.getFirstLevelName().equals("伤病治疗")){
                        StringUtils.copyProperties(s , p);
                    }
                });
            }
        });
        classReturn.forEach( p-> {
            if(p.getFirstLevelName().equals("身体准备训练")){
                // 查出除了 水上专项训练和陆上专项训练的其它数据
                if(classifiedTotalVOListTwo.size() > 0)classifiedTotalVOListTwo.forEach( s->{
                    if(s.getFirstLevelName().equals("身体准备训练")){
                        StringUtils.copyProperties(s , p);
                    }
                });
            }
        });
        classReturn.forEach( p-> {
            if(p.getFirstLevelName().equals("无训练")){
                // 查出除了 水上专项训练和陆上专项训练的其它数据
                if(classifiedTotalVOListTwo.size() > 0)classifiedTotalVOListTwo.forEach( s->{
                    if(s.getFirstLevelName().equals("无训练")){
                        StringUtils.copyProperties(s , p);
                    }
                });
            }
        });
        return sortingList;
    }
    /**
     * 运动员 -- 首页 --根据年查询各科训练时间占比--月统计
     */
    @Override
    public AjaxResult getHomeMonthlyStatistics(Integer userId , String years) {
        // 返回每个月的查询信息
        MonthVO monthVO = new MonthVO();

        // 查询 1 级科目
        List<TypeValueVO> typeValueVOS = dictDataService.getTypeValue("first_level");
        List<ClassifiedTotalVO> classifiedTotalVOList = new ArrayList<>();
        if(typeValueVOS.size() > 0){
            typeValueVOS.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classifiedTotalVOList.add(classifiedTotalVO);
            });
        }

        // 1 月的信息
        List<ClassifiedTotalVO> classifiedTotalVOS = trainingDayService.getClassifiedTotal(years+"-01" , userId);
        if(classifiedTotalVOS.size()>0){
            String nameList = "";
            for(int i = 0; i<classifiedTotalVOS.size(); i++){
                nameList+=classifiedTotalVOS.get(i).getFirstLevelName()+",";
            }

            // 重新组装数据
            List<ClassifiedTotalVO> classReturn = new ArrayList<>();
            classifiedTotalVOS.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                StringUtils.copyProperties(p , classifiedTotalVO);
                classReturn.add(classifiedTotalVO);
            });

            // 查询出没有的数据
            List<TypeValueVO> sysDictData = dictDataService.getIsNotTypeValue(nameList);
            sysDictData.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classReturn.add(classifiedTotalVO);
            });

            // 添加水上专项训练和陆上专项训练的其它数据
            if(this.adds(classReturn , years).size() > 0) this.adds(classReturn , years).forEach( c->{
                classReturn.add(c);
            });

            monthVO.setJanuary(classReturn);
        }else{
            monthVO.setJanuary(classifiedTotalVOList);
        }

        // 2 月的信息
        List<ClassifiedTotalVO> classifiedTotalVOS2 = trainingDayService.getClassifiedTotal(years+"-02" , userId);
        if(classifiedTotalVOS2.size()>0){
            String nameList = "";
            for(int i = 0; i<classifiedTotalVOS2.size(); i++){
                nameList+=classifiedTotalVOS2.get(i).getFirstLevelName()+",";
            }

            // 重新组装数据
            List<ClassifiedTotalVO> classReturn = new ArrayList<>();
            classifiedTotalVOS2.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                StringUtils.copyProperties(p , classifiedTotalVO);
                classReturn.add(classifiedTotalVO);
            });
            // 查询出没有的数据
            List<TypeValueVO> sysDictData = dictDataService.getIsNotTypeValue(nameList);
            sysDictData.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classReturn.add(classifiedTotalVO);
            });

            // 添加水上专项训练和陆上专项训练的其它数据
            if(this.adds(classReturn , years).size() > 0) this.adds(classReturn , years).forEach( c->{
                classReturn.add(c);
            });

            monthVO.setFebruary(classReturn);
        }else{
            monthVO.setFebruary(classifiedTotalVOList);
        }

        // 3 月的信息
        List<ClassifiedTotalVO> classifiedTotalVOS3 = trainingDayService.getClassifiedTotal(years+"-03" , userId);
        if(classifiedTotalVOS3.size()>0){
            String nameList = "";
            for(int i = 0; i<classifiedTotalVOS3.size(); i++){
                nameList+=classifiedTotalVOS3.get(i).getFirstLevelName()+",";
            }

            // 重新组装数据
            List<ClassifiedTotalVO> classReturn = new ArrayList<>();
            classifiedTotalVOS3.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                StringUtils.copyProperties(p , classifiedTotalVO);
                classReturn.add(classifiedTotalVO);
            });
            // 查询出没有的数据
            List<TypeValueVO> sysDictData = dictDataService.getIsNotTypeValue(nameList);
            sysDictData.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classReturn.add(classifiedTotalVO);
            });

            // 添加水上专项训练和陆上专项训练的其它数据
            if(this.adds(classReturn , years).size() > 0) this.adds(classReturn , years).forEach( c->{
                classReturn.add(c);
            });

            monthVO.setMarch(classReturn);
        }else{
            monthVO.setMarch(classifiedTotalVOList);
        }

        // 4 月的信息
        List<ClassifiedTotalVO> classifiedTotalVOS4 = trainingDayService.getClassifiedTotal(years+"-04" , userId);
        if(classifiedTotalVOS4.size()>0){
            String nameList = "";
            for(int i = 0; i<classifiedTotalVOS4.size(); i++){
                nameList+=classifiedTotalVOS4.get(i).getFirstLevelName()+",";
            }

            // 重新组装数据
            List<ClassifiedTotalVO> classReturn = new ArrayList<>();
            classifiedTotalVOS4.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                StringUtils.copyProperties(p , classifiedTotalVO);
                classReturn.add(classifiedTotalVO);
            });
            // 查询出没有的数据
            List<TypeValueVO> sysDictData = dictDataService.getIsNotTypeValue(nameList);
            sysDictData.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classReturn.add(classifiedTotalVO);
            });

            // 添加水上专项训练和陆上专项训练的其它数据
            if(this.adds(classReturn , years).size() > 0) this.adds(classReturn , years).forEach( c->{
                classReturn.add(c);
            });

            monthVO.setApril(classReturn);
        }else{
            monthVO.setApril(classifiedTotalVOList);
        }

        // 5 月的信息
        List<ClassifiedTotalVO> classifiedTotalVOS5 = trainingDayService.getClassifiedTotal(years+"-05" , userId);
        if(classifiedTotalVOS5.size()>0){
            String nameList = "";
            for(int i = 0; i<classifiedTotalVOS5.size(); i++){
                nameList+=classifiedTotalVOS5.get(i).getFirstLevelName()+",";
            }

            // 重新组装数据
            List<ClassifiedTotalVO> classReturn = new ArrayList<>();
            classifiedTotalVOS5.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                StringUtils.copyProperties(p , classifiedTotalVO);
                classReturn.add(classifiedTotalVO);
            });
            // 查询出没有的数据
            List<TypeValueVO> sysDictData = dictDataService.getIsNotTypeValue(nameList);
            sysDictData.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classReturn.add(classifiedTotalVO);
            });

            // 添加水上专项训练和陆上专项训练的其它数据
            if(this.adds(classReturn , years).size() > 0) this.adds(classReturn , years).forEach( c->{
                classReturn.add(c);
            });

            monthVO.setMay(classReturn);
        }else{
            monthVO.setMay(classifiedTotalVOList);
        }

        // 6 月的信息
        List<ClassifiedTotalVO> classifiedTotalVOS6 = trainingDayService.getClassifiedTotal(years+"-06" , userId);
        if(classifiedTotalVOS6.size()>0){
            String nameList = "";
            for(int i = 0; i<classifiedTotalVOS6.size(); i++){
                nameList+=classifiedTotalVOS6.get(i).getFirstLevelName()+",";
            }

            // 重新组装数据
            List<ClassifiedTotalVO> classReturn = new ArrayList<>();
            classifiedTotalVOS6.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                StringUtils.copyProperties(p , classifiedTotalVO);
                classReturn.add(classifiedTotalVO);
            });
            // 查询出没有的数据
            List<TypeValueVO> sysDictData = dictDataService.getIsNotTypeValue(nameList);
            sysDictData.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classReturn.add(classifiedTotalVO);
            });

            // 添加水上专项训练和陆上专项训练的其它数据
            if(this.adds(classReturn , years).size() > 0) this.adds(classReturn , years).forEach( c->{
                classReturn.add(c);
            });

            monthVO.setJune(classReturn);
        }else{
            monthVO.setJune(classifiedTotalVOList);
        }

        // 7 月的信息
        List<ClassifiedTotalVO> classifiedTotalVOS7 = trainingDayService.getClassifiedTotal(years+"-07" , userId);
        if(classifiedTotalVOS7.size()>0){
            String nameList = "";
            for(int i = 0; i<classifiedTotalVOS7.size(); i++){
                nameList+=classifiedTotalVOS7.get(i).getFirstLevelName()+",";
            }

            // 重新组装数据
            List<ClassifiedTotalVO> classReturn = new ArrayList<>();
            classifiedTotalVOS7.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                StringUtils.copyProperties(p , classifiedTotalVO);
                classReturn.add(classifiedTotalVO);
            });
            // 查询出没有的数据
            List<TypeValueVO> sysDictData = dictDataService.getIsNotTypeValue(nameList);
            sysDictData.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classReturn.add(classifiedTotalVO);
            });

            // 添加水上专项训练和陆上专项训练的其它数据
            if(this.adds(classReturn , years).size() > 0) this.adds(classReturn , years).forEach( c->{
                classReturn.add(c);
            });

            monthVO.setJuly(classReturn);
        }else{
            monthVO.setJuly(classifiedTotalVOList);
        }

        // 8 月的信息
        List<ClassifiedTotalVO> classifiedTotalVOS8 = trainingDayService.getClassifiedTotal(years+"-08" , userId);
        if(classifiedTotalVOS8.size()>0){
            String nameList = "";
            for(int i = 0; i<classifiedTotalVOS8.size(); i++){
                nameList+=classifiedTotalVOS8.get(i).getFirstLevelName()+",";
            }

            // 重新组装数据
            List<ClassifiedTotalVO> classReturn = new ArrayList<>();
            classifiedTotalVOS8.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                StringUtils.copyProperties(p , classifiedTotalVO);
                classReturn.add(classifiedTotalVO);
            });
            // 查询出没有的数据
            List<TypeValueVO> sysDictData = dictDataService.getIsNotTypeValue(nameList);
            sysDictData.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classReturn.add(classifiedTotalVO);
            });

            // 添加水上专项训练和陆上专项训练的其它数据
            if(this.adds(classReturn , years).size() > 0) this.adds(classReturn , years).forEach( c->{
                classReturn.add(c);
            });

            monthVO.setAugust(classReturn);
        }else{
            monthVO.setAugust(classifiedTotalVOList);
        }

        // 9 月的信息
        List<ClassifiedTotalVO> classifiedTotalVOS9 = trainingDayService.getClassifiedTotal(years+"-09" , userId);
        if(classifiedTotalVOS9.size()>0){
            String nameList = "";
            for(int i = 0; i<classifiedTotalVOS9.size(); i++){
                nameList+=classifiedTotalVOS9.get(i).getFirstLevelName()+",";
            }

            // 重新组装数据
            List<ClassifiedTotalVO> classReturn = new ArrayList<>();
            classifiedTotalVOS9.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                StringUtils.copyProperties(p , classifiedTotalVO);
                classReturn.add(classifiedTotalVO);
            });
            // 查询出没有的数据
            List<TypeValueVO> sysDictData = dictDataService.getIsNotTypeValue(nameList);
            sysDictData.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classReturn.add(classifiedTotalVO);
            });

            // 添加水上专项训练和陆上专项训练的其它数据
            if(this.adds(classReturn , years).size() > 0) this.adds(classReturn , years).forEach( c->{
                classReturn.add(c);
            });

            monthVO.setSeptember(classReturn);
        }else{
            monthVO.setSeptember(classifiedTotalVOList);
        }

        // 10 月的信息
        List<ClassifiedTotalVO> classifiedTotalVOS10 = trainingDayService.getClassifiedTotal(years+"-10" , userId);
        if(classifiedTotalVOS10.size()>0){
            String nameList = "";
            for(int i = 0; i<classifiedTotalVOS10.size(); i++){
                nameList+=classifiedTotalVOS10.get(i).getFirstLevelName()+",";
            }

            // 重新组装数据
            List<ClassifiedTotalVO> classReturn = new ArrayList<>();
            classifiedTotalVOS10.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                StringUtils.copyProperties(p , classifiedTotalVO);
                classReturn.add(classifiedTotalVO);
            });
            // 查询出没有的数据
            List<TypeValueVO> sysDictData = dictDataService.getIsNotTypeValue(nameList);
            sysDictData.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classReturn.add(classifiedTotalVO);
            });

            // 添加水上专项训练和陆上专项训练的其它数据
            if(this.adds(classReturn , years).size() > 0) this.adds(classReturn , years).forEach( c->{
                classReturn.add(c);
            });

            monthVO.setOctober(classReturn);
        }else{
            monthVO.setOctober(classifiedTotalVOList);
        }

        // 11 月的信息
        List<ClassifiedTotalVO> classifiedTotalVOS11 = trainingDayService.getClassifiedTotal(years+"-11" , userId);
        if(classifiedTotalVOS11.size()>0){
            String nameList = "";
            for(int i = 0; i<classifiedTotalVOS11.size(); i++){
                nameList+=classifiedTotalVOS11.get(i).getFirstLevelName()+",";
            }

            // 重新组装数据
            List<ClassifiedTotalVO> classReturn = new ArrayList<>();
            classifiedTotalVOS11.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                StringUtils.copyProperties(p , classifiedTotalVO);
                classReturn.add(classifiedTotalVO);
            });
            // 查询出没有的数据
            List<TypeValueVO> sysDictData = dictDataService.getIsNotTypeValue(nameList);
            sysDictData.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classReturn.add(classifiedTotalVO);
            });

            // 添加水上专项训练和陆上专项训练的其它数据
            if(this.adds(classReturn , years).size() > 0) this.adds(classReturn , years).forEach( c->{
                classReturn.add(c);
            });

            monthVO.setNovember(classReturn);
        }else{
            monthVO.setNovember(classifiedTotalVOList);
        }

        // 12 月的信息
        List<ClassifiedTotalVO> classifiedTotalVOS12 = trainingDayService.getClassifiedTotal(years+"-12" , userId);
        if(classifiedTotalVOS12.size()>0){
            String nameList = "";
            for(int i = 0; i<classifiedTotalVOS12.size(); i++){
                nameList+=classifiedTotalVOS12.get(i).getFirstLevelName()+",";
            }

            // 重新组装数据
            List<ClassifiedTotalVO> classReturn = new ArrayList<>();
            classifiedTotalVOS12.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                StringUtils.copyProperties(p , classifiedTotalVO);
                classReturn.add(classifiedTotalVO);
            });
            // 查询出没有的数据
            List<TypeValueVO> sysDictData = dictDataService.getIsNotTypeValue(nameList);
            sysDictData.forEach( p-> {
                ClassifiedTotalVO classifiedTotalVO = new ClassifiedTotalVO();
                classifiedTotalVO.setFirstLevelName(p.getDictLabel());
                classifiedTotalVO.setTotalTime(0.0);
                classifiedTotalVO.setDay("");
                classifiedTotalVO.setSecondLevelName("");
                classifiedTotalVO.setTotalKilometer(0.0);
                classReturn.add(classifiedTotalVO);
            });

            // 添加水上专项训练和陆上专项训练的其它数据
            if(this.adds(classReturn , years).size() > 0) this.adds(classReturn , years).forEach( c->{
                classReturn.add(c);
            });

            monthVO.setDecember(classReturn);
        }else{
            monthVO.setDecember(classifiedTotalVOList);
        }

        return AjaxResult.success(monthVO);
    }


    /**
     * 运动员 -- 水上专项成绩排名
     */
    @Override
    public AjaxResult getThis(Integer sex , Integer trainingMode , Integer kilometers , Integer realClassHour) {
        Integer ranking = 0;
        List<WaterRankingVO> waterRankingVOList = new ArrayList<>();
        List<WaterRankingVO> waterRankingVOListTwo = trainingAchievementService.getWaterRankingVO(null , trainingMode , kilometers , sex , realClassHour);
        // 获取所有人的名称
        List<String> nameList = new ArrayList<>();
        for(int i = 0; i<waterRankingVOListTwo.size(); i++){
            WaterRankingVO waterRankingVO = new WaterRankingVO();
            StringUtils.copyProperties(waterRankingVOListTwo.get(i) , waterRankingVO);
            waterRankingVO.setRanking(ranking+=1);
            waterRankingVOList.add(waterRankingVO);
            nameList.add(waterRankingVOListTwo.get(i).getName());
        }

        // 删除重复的 list
        List<String> newList = new  ArrayList<>();
        for (String cd:nameList) {
            if(!newList.contains(cd)){
                newList.add(cd);
            }
        }

        List<WaterRankingVO> waterRankingVOListTwo2 = new ArrayList<>();
        for(int i = 0; i<newList.size(); i++){
            List<WaterRankingVO> waterRankingNameVO = trainingAchievementService.getWaterRankingNameVO(newList.get(i) , trainingMode , kilometers , sex , realClassHour);
            // 根据 运动员名称查询排名
            WaterRankingVO waterRankingVO = new WaterRankingVO();
            if(waterRankingNameVO.size() > 0){
                StringUtils.copyProperties(waterRankingNameVO.get(0) , waterRankingVO);
            }
            waterRankingVO.setRanking(i+1);
            waterRankingVOListTwo2.add(waterRankingVO);
        }

        if(waterRankingVOList.size() > 0 && waterRankingVOList.size() <= 3){
            return AjaxResult.success(PageUtil.startPage(waterRankingVOListTwo2 ,1,waterRankingVOList.size()));
        }else if(waterRankingVOList.size() > 3) {
            return AjaxResult.success(PageUtil.startPage(waterRankingVOListTwo2 ,1,3));
        }else{
            return AjaxResult.success("暂无数据！");
        }
    }


    /**
     * 运动员 -- 陆上专项成绩排名
     */
    @Override
    public AjaxResult getThisOnLand(Integer sex , Integer trainingMode, Integer kilometers , Integer realClassHour) {
        Integer ranking = 0;
        List<WaterRankingVO> waterRankingVOList = new ArrayList<>();
        List<WaterRankingVO> waterRankingVOListTwo = trainingAchievementService.getWaterRankingVOLU(null , trainingMode , kilometers , sex , realClassHour);
        // 获取所有人的名称
        List<String> nameList = new ArrayList<>();
        for(int i = 0; i<waterRankingVOListTwo.size(); i++){
            WaterRankingVO waterRankingVO = new WaterRankingVO();
            StringUtils.copyProperties(waterRankingVOListTwo.get(i) , waterRankingVO);
            waterRankingVO.setRanking(ranking+=1);
            waterRankingVOList.add(waterRankingVO);
            nameList.add(waterRankingVOListTwo.get(i).getName());
        }

        // 删除重复的 list
        List<String> newList = new  ArrayList<>();
        for (String cd:nameList) {
            if(!newList.contains(cd)){
                newList.add(cd);
            }
        }

        List<WaterRankingVO> waterRankingVOListTwo2 = new ArrayList<>();
        for (int i = 0; i < newList.size(); i++) {
            // 根据 运动员名称查询排名
            WaterRankingVO waterRankingVO = new WaterRankingVO();
            StringUtils.copyProperties(trainingAchievementService.getWaterRankingNameVOLU(newList.get(i), trainingMode, kilometers, sex, realClassHour).get(0), waterRankingVO);
            waterRankingVO.setRanking(i + 1);
            waterRankingVOListTwo2.add(waterRankingVO);
        }

        if(waterRankingVOList.size() > 0 && waterRankingVOList.size() <= 3){
            return AjaxResult.success(PageUtil.startPage(waterRankingVOListTwo2 ,1,waterRankingVOList.size()));
        }else if(waterRankingVOList.size() > 3) {
            return AjaxResult.success(PageUtil.startPage(waterRankingVOListTwo2 ,1,3));
        }else{
            return AjaxResult.success("暂无数据！");
        }
    }


    /**
     * 运动员 -- 首页 -- 水上专项滑行统计 Sum
     */
    @Override
    public AjaxResult getWaterGlidingSum(String year , Integer userId , Integer trainingMode) {
        // 1，查询当前运动员 请假的时间
        List<String> dayList = new ArrayList<>();
        List<LeaveForm> leaveFormList = leaveFormService.list(Wrappers.<LeaveForm>lambdaQuery()
                .eq(LeaveForm::getSportsmanId , userId));
        if(leaveFormList.size()>0){
            leaveFormList.forEach( p-> {
                dayList.add(p.getEndTime().toString());
            });
        }
        return AjaxResult.success(trainingDayService.returnMonthTimeVO(year , userId , trainingMode , dayList));
    }




    /**
     * 封装查询方法
     * 运动员 -- 首页 -- 陆上专项滑行统计 Sum
     */
    public double getLandGlidingSumTow(String year , Integer userId , Integer trainingMode , List<String> dayList){
        // 根据成绩表查询今天参加的所有课程 id
        List<Integer> idList = trainingAchievementService.getIdList(userId , year);

        // 查询今天的课程 所有的热身距离然后相加
        double warmUpKilometers = 0.0;
        if(idList.size() > 0){
            List<TrainingDay> trainingDayList = trainingDayService.list(Wrappers.<TrainingDay>lambdaQuery()
                    .in(TrainingDay::getId , idList));
            for (int i = 0; i<trainingDayList.size(); i++){
                warmUpKilometers += trainingDayList.get(i).getWarmUpKilometers();
            }
        }

        List<GetLandGlidingSumVO> getLandGlidingSumVOList = trainingDayService.getLandGlidingSum(year , userId , trainingMode , dayList);

        double landGlidingSum = 0.0;
        if(StringUtils.isNotNull(getLandGlidingSumVOList.get(0)) && getLandGlidingSumVOList.size() > 0){
            landGlidingSum = getLandGlidingSumVOList.get(0).getLandGlidingSum();
            return landGlidingSum + warmUpKilometers;
        }else{
            return landGlidingSum + warmUpKilometers;
        }
    }

    /**
     * 运动员 -- 首页 -- 陆上专项滑行统计 Sum
     */
    @Override
    public AjaxResult getLandGlidingSum(String year , Integer userId , Integer trainingMode) {
        // 1，查询当前运动员 请假的时间
        List<String> dayList = new ArrayList<>();
        List<LeaveForm> leaveFormList = leaveFormService.list(Wrappers.<LeaveForm>lambdaQuery()
                .eq(LeaveForm::getSportsmanId , userId));
        if(leaveFormList.size()>0){
            leaveFormList.forEach( p-> {
                dayList.add(p.getEndTime().toString());
            });
        }
        return AjaxResult.success(getLandGlidingSumTow(year , userId , trainingMode , dayList));
    }


    /**
     * 运动员 -- 首页 -- 专项滑行折线图
     */
    @Override
    public AjaxResult getSpecialTaxiingLineChart(String year , Integer userId , Integer trainingMode) {

        // 1，查询当前运动员 请假的时间
        List<String> dayList = new ArrayList<>();
        List<LeaveForm> leaveFormList = leaveFormService.list(Wrappers.<LeaveForm>lambdaQuery()
                .eq(LeaveForm::getSportsmanId , userId));
        if(leaveFormList.size()>0){
            leaveFormList.forEach( p-> {
                dayList.add(p.getEndTime().toString());
            });
        }

        // 封装 运动员 -- 首页 -- 专项滑行折线图
        SpecialTaxiingLineChartVO specialVO = new SpecialTaxiingLineChartVO();

        // 水上折线图
        ReturnMonthVO water = new ReturnMonthVO();
        // 1月
        water.setJanuary(trainingDayService.returnMonthTimeVO(year+"-01" , userId , trainingMode , dayList));

        // 2月
        water.setFebruary(trainingDayService.returnMonthTimeVO(year+"-02" , userId , trainingMode , dayList));

        // 3月
        water.setMarch(trainingDayService.returnMonthTimeVO(year+"-03" , userId , trainingMode , dayList));

        // 4月
        water.setApril(trainingDayService.returnMonthTimeVO(year+"-04" , userId , trainingMode , dayList));

        // 5月
        water.setMay(trainingDayService.returnMonthTimeVO(year+"-05" , userId , trainingMode , dayList));

        // 6月
        water.setJune(trainingDayService.returnMonthTimeVO(year+"-06" , userId , trainingMode , dayList));

        // 7月
        water.setJuly(trainingDayService.returnMonthTimeVO(year+"-07" , userId , trainingMode , dayList));

        // 8月
        water.setAugust(trainingDayService.returnMonthTimeVO(year+"-08" , userId , trainingMode , dayList));

        // 9月
        water.setSeptember(trainingDayService.returnMonthTimeVO(year+"-09" , userId , trainingMode , dayList));

        // 10月
        water.setOctober(trainingDayService.returnMonthTimeVO(year+"-10" , userId , trainingMode , dayList));

        // 11月
        water.setNovember(trainingDayService.returnMonthTimeVO(year+"-11" , userId , trainingMode , dayList));

        // 12月
        water.setDecember(trainingDayService.returnMonthTimeVO(year+"-12" , userId , trainingMode , dayList));

        // 存入 水上运动折线图
        specialVO.setWater(water);

        // 陆上折线图
        ReturnMonthVO land = new ReturnMonthVO();
        // 1月
        land.setJanuary(getLandGlidingSumTow(year+"-01" , userId , trainingMode , dayList));

        // 2月
        land.setFebruary(getLandGlidingSumTow(year+"-02" , userId , trainingMode , dayList));

        // 3月
        land.setMarch(getLandGlidingSumTow(year+"-03" , userId , trainingMode , dayList));

        // 4月
        land.setApril(getLandGlidingSumTow(year+"-04" , userId , trainingMode , dayList));

        // 5月
        land.setMay(getLandGlidingSumTow(year+"-05" , userId , trainingMode , dayList));

        // 6月
        land.setJune(getLandGlidingSumTow(year+"-06" , userId , trainingMode , dayList));

        // 7月
        land.setJuly(getLandGlidingSumTow(year+"-07" , userId , trainingMode , dayList));

        // 8月
        land.setAugust(getLandGlidingSumTow(year+"-08" , userId , trainingMode , dayList));

        // 9月
        land.setSeptember(getLandGlidingSumTow(year+"-09" , userId , trainingMode , dayList));

        // 10月
        land.setOctober(getLandGlidingSumTow(year+"-10" , userId , trainingMode , dayList));

        // 11月
        land.setNovember(getLandGlidingSumTow(year+"-11" , userId , trainingMode , dayList));

        // 12月
        land.setDecember(getLandGlidingSumTow(year+"-12" , userId , trainingMode , dayList));

        // 存入 陆上运动折线图
        specialVO.setLand(land);

        return AjaxResult.success(specialVO);
    }







}
